﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MShop.Core.DataAccess;
using MShop.Core.Domain;
using MShop.Core;


namespace MShop.Web.Areas.Admin.Controllers
{
    public class ItemCatPropController : SecureController
    {
        private readonly IItemCatDao _itemCatDao;
        private readonly IItemPropDao _itemPropDao;
        private readonly IItemCatPropDao _itemCatPropDao;
        public ItemCatPropController(IItemCatDao itemCatDao, IItemPropDao itemPropDao, IItemCatPropDao itemCatPropDao)
        {
            this._itemCatPropDao = itemCatPropDao;
            this._itemCatDao = itemCatDao;
            this._itemPropDao = itemPropDao;
            
        }

        public ActionResult Index(int id)
        {
            ViewData["list"] = _itemCatPropDao.Find(x => x.ItemCatId == id);
            ViewData["itemcats"] = _itemCatDao.Build();
            ViewData["listprop"] = _itemPropDao.GetAll().ToList();
           
       
            return View();
        }

        public ActionResult Add(int pid,int id)
        {
                var itemcatprop=new ItemCatProp(){ ItemCatId =id,ItemPropId =pid};
                if (_itemCatPropDao.FirstOrDefault(x => x.ItemCatId == itemcatprop.ItemCatId && x.ItemPropId == itemcatprop.ItemPropId)==null)
                {
                    _itemCatPropDao.Save(itemcatprop);
                    _itemCatPropDao.SaveChanges();
                }               
                    
               return RedirectToAction("index", new { id = id });
        }


        public ActionResult Delete(int id,int itemcatid)
        {
            var itemcatprop = _itemCatPropDao.FirstOrDefault(x => x.Id == id);
            _itemCatPropDao.Delete(itemcatprop);
            _itemCatPropDao.SaveChanges();
            return RedirectToAction("index", new { id = itemcatid });
        }
        

    }
}
